const url = require('url')

class Router {
  routes = []

  add(path, method, handlers) {
    handlers.forEach(handler=>{
      this.routes.push({
        path,
        method,
        handler
      })
    })
  }

  action(req, res, done) {
    let { pathname } = url.parse(req.url, true)
    let index = 0
    const next = () => {
      // 找出口
      if (index > this.routes.length) return done()
      let route = this.routes[index++]
      if (route.path === pathname) {
        route.handler(req, res, next)
      } else {
        next()
      }
    }
    next()
  }

}

module.exports = Router